﻿#region Copyright ©2008-2011, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-06-15 08:45:45 +0200 (Mon, 15 Jun 2009) $
// Last Changed Revision $Rev: 194 $
//
// ====================================================================================================
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Tum.CollabXT.Wizard
{
    /// <summary>
    /// Interaction logic for SelectToolProviders.xaml
    /// </summary>
    internal partial class SelectToolProvidersPage : InternalConversionWorkflowPage
    {
        /// <summary>
        /// Conversion workflow.
        /// </summary>
        ConversionWorkflow Workflow;


        /// <summary>
        /// Constructor.
        /// </summary>
        public SelectToolProvidersPage()
        {
            InitializeComponent();

            Helper.FillInlineCollectionFromString(textBlockSelectToolProviders.Inlines, Wizard.Resources.Language.ToolProvidersPage_SelectToolProviders);
        }

        /// <summary>
        /// Receives the conversion workflow.
        /// </summary>
        /// <param name="workflow">Conversion workflow.</param>
        /// <returns>True, if the workflow has been received correctly, false if not.</returns>
        public override bool InternalReceiveWorkflowHandle(ConversionWorkflow workflow)
        {
            Workflow = workflow;

            PopulateProviderList();
            return true;
        }

        /// <summary>
        /// Next page has been requested.
        /// </summary>
        /// <returns>True, if the page change is allowed (tool providers have been selected), false if not.</returns>
        public override bool NextPageRequested()
        {
            if (Workflow == null)
                return true;

            return Workflow.Project.ProcessProvider != null;
        }

        /// <summary>
        /// Populates the tool provider list.
        /// </summary>
        private void PopulateProviderList()
        {
            Workflow.UpdateButtonState(true, false);

            stackPanelProviders.Children.Clear();
            foreach (ToolProviderInfo curProviderInfo in Workflow.PluginMgr.GetToolProviders())
            {
                //Create provider radio button
                CheckBox providerBox = new CheckBox();
                providerBox.Content = curProviderInfo.Name;
                providerBox.FontWeight = FontWeight.FromOpenTypeWeight(700);
                providerBox.Tag = curProviderInfo;
                providerBox.Checked += new RoutedEventHandler(ProviderCheckBoxCheckedHandler);
                providerBox.Unchecked += new RoutedEventHandler(ProviderCheckBoxCheckedHandler);
                providerBox.IsChecked = Workflow.Project.ToolProviders.Contains(curProviderInfo);

                //Create description text
                TextBlock descriptionTextBlock = new TextBlock();
                descriptionTextBlock.Text = curProviderInfo.Description;
                descriptionTextBlock.Margin = new Thickness(15, 0, 0, 0);
                descriptionTextBlock.TextWrapping = TextWrapping.Wrap;
                descriptionTextBlock.Tag = providerBox;
                descriptionTextBlock.MouseLeftButtonDown += new MouseButtonEventHandler(descriptionTextBlock_MouseLeftButtonDown);

                //Add both
                stackPanelProviders.Children.Add(providerBox);
                stackPanelProviders.Children.Add(descriptionTextBlock);
            }
        }

        /// <summary>
        /// Event handler: Description mouse down.
        /// </summary>
        void descriptionTextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Text has been clicked - change check status
            TextBlock senderBlock = sender as TextBlock;
            if (senderBlock.Tag is CheckBox)
                ((CheckBox)senderBlock.Tag).IsChecked = !((CheckBox)senderBlock.Tag).IsChecked;
        }

        /// <summary>
        /// Event handler: Provider check box.
        /// </summary>
        public void ProviderCheckBoxCheckedHandler(Object sender, RoutedEventArgs e)
        {
            if (((CheckBox)sender).IsChecked == true)
            {
                if (!Workflow.Project.ToolProviders.Contains(((CheckBox)sender).Tag as ToolProviderInfo))
                {
                    Workflow.Project.ToolProviders.Add(((CheckBox)sender).Tag as ToolProviderInfo);
                    Workflow.InvalidateProcessing();
                }
            }
            else
            {
                Workflow.Project.ToolProviders.Remove(((CheckBox)sender).Tag as ToolProviderInfo);
                Workflow.InvalidateProcessing();
            }

            //Enable next
            Workflow.UpdateButtonState(true, Workflow.Project.ToolProviders.Count > 0);
        }
    }
}
